import { Component, OnInit } from '@angular/core';
import {PageEntity} from '../../../core/domain/page.entity';
import {EvaluatedService} from './evaluated.service';
import {NzMessageService} from 'ng-zorro-antd';
import {PaginationUtils} from '../../../utils/paginationUtils';
import {FilterEnum} from '../../../core/enum/filter.enum';
import {StatusEnum} from '../../../core/enum/status.enum';

@Component({
  selector: 'app-evaluated',
  templateUrl: './evaluated.component.html',
  styleUrls: ['./evaluated.component.scss'],
  providers: [EvaluatedService]
})
export class EvaluatedComponent implements OnInit {
  pageInfo: PageEntity = new PageEntity();
  yearList = [];
  semesterList = [];
  searchParams: SearchParams = new SearchParams();
  dataList = [];
  view = false;
  nzWidthConfigs = [];
  evaluateObj: any = {};
  questionList = [];
  constructor(
    private http: EvaluatedService,
    private message: NzMessageService
  ) { }

  ngOnInit() {
    this.getSchoolYearList();
  }

  /**
   * 获取学年学期
   */
  getSchoolYearList() {
    this.http.getSchoolYearApi().subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.yearList = res.datas;
        res.datas.map(item => {
          if (item.currSchoolYear) {
            // this.searchParams.schoolYear = item.schoolYear;
            this.semesterList = item.semester;
            item.semester.map(ele => {
              if (ele.currSemester) {
                // this.searchParams.semester = ele.semester;
              }
            });
          }
        });
        this.clickSearch();
      }
    });
  }

  page(page) {
    this.pageInfo.filters.state = PaginationUtils.filters('已评价', FilterEnum.EQUALS);
    this.pageInfo.multiSortMeta = [
      {
        field: 'gmtCreate',
        order: -1
      }
    ];
    this.http.page(page).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.dataList = res.datas.data;
        this.pageInfo.totalRecords = res.datas.totalRecords;
        this.nzWidthConfigs = ['60px', '150px', '100px', '150px', '150px',
          '100px', '100px', '180px', '100px', '100px',
          '100px',  '150px',
          '200px', '200px', '100px', '100px'];
      }
    });
  }

  /**
   * 点击查询
   */
  clickSearch() {
    for (const o in this.searchParams) {
      if (this.searchParams[o]) {
        this.pageInfo.filters[o] = PaginationUtils.filters(this.searchParams[o], FilterEnum.CONTAINS);
      } else {
        delete this.pageInfo.filters[o];
      }
      if (this.searchParams.semester === 0) {
        this.pageInfo.filters.semester = PaginationUtils.filters(this.searchParams.semester, FilterEnum.EQUALS);
      }
    }
    this.pageInfo.first = 1;
    this.page(this.pageInfo);
  }

  /**
   * 点击重置
   */
  resetSearch() {
    this.searchParams = new SearchParams();
    this.pageInfo = new PageEntity();
    this.clickSearch();
  }

  changePageIndex(index) {
    this.pageInfo.first = index;
    this.page(this.pageInfo);
  }

  changePageSize(size) {
    this.pageInfo.first = 1;
    this.pageInfo.rows = size;
    this.clickSearch();
  }

  /**
   * 点击详情
   */
  clickEvaluateInfo(data) {
    this.http.getEvaluateInfoApi({
      id: data.id
    }).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
       this.view = true;
       this.evaluateObj = res.datas;
       this.questionList = res.datas.detailList;
      }
    });
  }

}
class SearchParams {
  schoolYear: string;
  semester: any;
  number: string;
  name: string;
}
